from flask import Flask, render_template, request
import requests
from bs4 import BeautifulSoup

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/scrape', methods=['POST'])
def scrape():
    url = request.form['url']
    user_name = request.form['username']
    user_password = request.form['password']
    
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36",
    }
    
    # 登录
    login_url = url + '/login'
    login_data = {
        'username': user_name,
        'password': user_password,  # 'captcha': 'captcha_value',  # 如果有验证码，需要填写验证码值
    }
    
    # 发送登录请求
    session = requests.Session()
    response = session.post(login_url, data=login_data, headers=headers)
    
    if response.ok:
        # 登录成功，进行抢课逻辑
        # 这里可以添加抢课的代码
        return '<h1>登录成功！</h1>'
    else:
        return '<h1>登录失败，请检查您的用户名和密码。</h1>'

if __name__ == '__main__':
    app.run(debug=True)
